package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/ifnk/micro-blog/internal/blog/global"
	"github.com/ifnk/micro-blog/internal/pkg/base"
	response "github.com/ifnk/micro-blog/internal/pkg/http"
	"github.com/ifnk/micro-blog/protobuf/dict_pb"
	"github.com/infobloxopen/atlas-app-toolkit/query"
	"strconv"
	"strings"
)

func InitDictDetailController(r *gin.RouterGroup) {
	api := r.Group("/dictDetail")
	// 这个 要 使用 jwt 和 casbin 中间件
	//api.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()) // 在这行之前的api 裸奔,之后的受保护
	api.GET("", getDictDetails)
	api.GET("/:id", getDictDetail)
	api.POST("", createDictDetail)
	api.PUT("/:id", updateDictDetail)
	api.DELETE("/:id", deleteDictDetail)
}

func createDictDetail(c *gin.Context) {
	var input dict_pb.DictDetail
	_ = c.ShouldBindJSON(&input)

	res, err := global.DictDetailClient.Create(c, &dict_pb.CreateDictDetailRequest{Payload: &input})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	orm, err := res.GetResult().ToORM(c)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(orm, c)
}

func getDictDetail(c *gin.Context) {
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	res, err := global.DictDetailClient.Read(c, &dict_pb.ReadDictDetailRequest{Id: uint64(id)})
	orm, err := res.GetResult().ToORM(c)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(orm, c)
}
func deleteDictDetail(c *gin.Context) {
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	res, err := global.DictDetailClient.Delete(c, &dict_pb.DeleteDictDetailRequest{Id: uint64(id)})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(res, c)
}
func updateDictDetail(c *gin.Context) {
	var input dict_pb.DictDetailORM
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	_ = c.ShouldBindJSON(&input)
	pb, _ := input.ToPB(c)
	pb.Id = uint64(id)
	res, err := global.DictDetailClient.Update(c, &dict_pb.UpdateDictDetailRequest{Payload: &pb})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	orm, _ := res.GetResult().ToORM(c)
	response.OkWithData(orm, c)
}
func getDictDetails(c *gin.Context) {
	var params base.PageEntity
	_ = c.ShouldBindQuery(&params)
	offset := int32(params.PageIndex-1) * int32(params.PageSize)
	limit := int32(params.PageSize)
	menuRes, err := global.DictDetailClient.List(c, &dict_pb.ListDictDetailRequest{Paging: &query.Pagination{Offset: offset, Limit: limit}})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	global.Logger.Infof("menuRes -> %+v", menuRes)

	var pbResponse []dict_pb.DictDetailORM
	for _, item := range menuRes.GetResults() {
		orm, err := item.ToORM(c)
		if err != nil {
			response.FailWithMessage(err.Error(), c)
			return
		}
		pbResponse = append(pbResponse, orm)
	}
	var total int32
	if menuRes.PageInfo == nil {
		total = 0
	} else {
		total = menuRes.PageInfo.Size
	}

	response.OkWithData(DictDetailPageRes{
		Total: total,
		Items: pbResponse,
	}, c)
}

type DictDetailPageRes struct {
	Total int32
	Items []dict_pb.DictDetailORM
}
